package Application.Tool;

import Application.AppJoint;
import Application.Controller.GrayPenSettingsController;
import Application.Mask.MaskManager;
import javafx.scene.Parent;
import javafx.fxml.FXMLLoader;
import Application.LayerImage.LayerImage;
import javafx.scene.input.MouseEvent;
import Application.Layer.Layer;
import Application.Layer.LayerDelegate;

public class GrayPen extends Tool {
    private Parent root;
    private static GrayPen ins = new GrayPen();

    private LayerImage mask;
    private int size = 32;

    public static GrayPen getInstance() {
        return ins;
    }

    private GrayPen() {
        super();
        // 加载本工具的工具设置
        try {
            FXMLLoader loader = new FXMLLoader(getClass().getResource("/View/GrayPenSettings.fxml"));
            root = loader.load();

            AppJoint.getInstance().loadMasksToComboBox(
                    ((GrayPenSettingsController)loader.getController()).getCombo()
            );
        } catch(Exception e) {
            e.printStackTrace();
        }

        mask = MaskManager.getInstance().getMaskByName("test");
    }

    public void setSize(int size) {
        this.size = size;
    }

    public void setMask(String maskName) {
        this.mask = MaskManager.getInstance().getMaskByName(maskName);
    }

    public void setAlgorithm(String algorithm) {

    }

    @Override public void onMouseMove(MouseEvent e) {
        double pixelx =
                e.getX() / AppJoint.getInstance().getImageView().getFitWidth() * AppJoint.getInstance().getCanvasWidth();
        double pixely =
                e.getY() / AppJoint.getInstance().getImageView().getFitHeight() * AppJoint.getInstance().getCanvasHeight();
        Layer activeLayer = LayerDelegate.getInstance().getLayerById(AppJoint.getInstance().getActiveLayerId());

        for(int i = -size / 2; i < size / 2; i++) {
            for(int j = -size / 2; j < size / 2; j++) {
                // 实际像素位置
                int imgx = (int)pixelx + i;
                int imgy = (int)pixely + j;
                if(imgx >= 0 && imgx < AppJoint.getInstance().getCanvasWidth() && imgy >= 0 && imgy < AppJoint.getInstance().getCanvasHeight()) {
                    // 蒙版像素位置
                    int maskx = (i + size / 2) * MaskManager.MASK_SIZE / size;
                    int masky = (j + size / 2) * MaskManager.MASK_SIZE / size;
                    // 蒙版灰度
                    int gray = (mask.getPixelArgb(maskx, masky) & 255);

                    int oldColor = activeLayer.getArgb(imgx, imgy);
                    int a = (oldColor >> 24) & 0xff;
                    int r = (oldColor & 0x00ff0000) >> 16;
                    int g = (oldColor & 0x0000ff00) >> 8;
                    int b = oldColor & 0x000000ff;
                    int ans = (r * 78381 + g * 153878 + b * 29885) >> 18;
                    //System.out.println(ans);
                    int newColor = (a << 24) | (ans << 16) | (ans << 8) | ans;
                    if(gray < 50) {
                        newColor = oldColor;
                    }

                    activeLayer.setArgb(imgx, imgy, newColor);
                    LayerDelegate.getInstance().refreshSingle(imgx, imgy);
                }
            }
        }
    }

    @Override public void onMouseDown(MouseEvent e) {
        onMouseMove(e);
    }

    @Override public void onMouseUp(MouseEvent e) {

    }

    @Override public Parent getToolSettings() {
        return root;
    }
}
